package com.simple.gateway.sdk.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.simple.gateway.sdk.common.Result;
import com.simple.gateway.sdk.exception.GatewayException;
import com.simple.gateway.sdk.service.param.ApplicationInterfaceMethodParam;
import com.simple.gateway.sdk.service.param.ApplicationInterfaceParam;
import com.simple.gateway.sdk.service.param.ApplicationSystemParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 项目: simple-gateway
 * <p>
 * 功能描述:
 *
 * @author: WuChengXing
 * @create: 2023-08-09 22:54
 **/
public class GatewayCenterService {

    private final Logger logger = LoggerFactory.getLogger(GatewayCenterService.class);

    public void doRegisterApplication(String address, ApplicationSystemParam param) {
        String resultStr;
        try {
            resultStr = HttpUtil.post(address + "/gateway/api/application/registerApplication", JSON.toJSONString(param), 3000);
        } catch (Exception e) {
            logger.error("应用服务注册异常，链接资源不可用：{}", address + "/gateway/api/application/registerApplication");
            return;
        }
        Result<Boolean> result = JSON.parseObject(resultStr, new TypeReference<Result<Boolean>>() {
        });
        logger.info("向网关中心注册应用服务 systemId：{} systemName：{} 注册结果：{}", param.getSystemId(), param.getSystemName(), resultStr);
        if (!"0000".equals(result.getCode()) && !"0003".equals(result.getCode())) {
            logger.error("注册应用服务异常 [systemId：" + param.getSystemId() + "] 、[systemRegistry：" + param.getSystemRegistry() + "]");
        }
    }

    public void doRegisterApplicationInterface(String address, ApplicationInterfaceParam param) {
        String resultStr;
        try {
            resultStr = HttpUtil.post(address + "/gateway/api/application/registerApplicationInterface", JSON.toJSONString(param), 550);
        } catch (Exception e) {
            logger.error("应用服务接口注册异常，链接资源不可用：{}", address + "/gateway/api/application/registerApplicationInterface");
            return;
        }
        Result<Boolean> result = JSON.parseObject(resultStr, new TypeReference<Result<Boolean>>() {
        });
        logger.info("向网关中心注册应用接口服务 systemId：{} interfaceId：{} interfaceName：{} 注册结果：{}",
                param.getSystemId(), param.getInterfaceId(), param.getInterfaceName(), resultStr);
        if (!"0000".equals(result.getCode()) && !"0003".equals(result.getCode())) {
            logger.error("向网关中心注册应用接口服务异常 [systemId："
                    + param.getSystemId() + "] 、[interfaceId："
                    + param.getInterfaceId() + "]");
        }
    }

    public void doRegisterApplicationInterfaceMethod(String address, ApplicationInterfaceMethodParam param) {

        String resultStr;
        try {
            resultStr = HttpUtil.post(address + "/gateway/api/application/registerApplicationInterfaceMethod",
                    JSON.toJSONString(param), 3000);
        } catch (Exception e) {
            logger.error("应用服务接口注册方法异常，链接资源不可用：{}", address + "/gateway/api/application/registerApplicationInterfaceMethod");
            return;
        }
        Result<Boolean> result = JSON.parseObject(resultStr, new TypeReference<Result<Boolean>>() {
        });
        logger.info("向网关中心注册应用接口方法服务 systemId：{} interfaceId：{} methodId：{} 注册结果：{}",
                param.getSystemId(), param.getInterfaceId(), param.getMethodId(), resultStr);
        if (!"0000".equals(result.getCode()) && !"0003".equals(result.getCode())) {
           logger.error("向网关中心注册应用接口方法服务异常 [systemId："
                    + param.getSystemId() + "] 、[interfaceId："
                    + param.getInterfaceId() + "]、[methodId：]"
                    + param.getMethodId() + "]");
        }
    }

    public void doRegisterEvent(String address, String systemId) {
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("systemId", systemId);
        String resultStr;
        try {
            resultStr = HttpUtil.post(address + "/gateway/api/config/registerEvent", paramMap, 3000);
        } catch (Exception e) {
            logger.error("应用服务接口事件方法异常，链接资源不可用：{}", address + "/gateway/api/config/registerEvent");
           return;
        }
        Result<Boolean> result = JSON.parseObject(resultStr, new TypeReference<Result<Boolean>>() {
        });
        logger.info("应用服务接口事件方法 systemId：{} 注册结果：{}", systemId, resultStr);
        if (!"0000".equals(result.getCode())) {
           logger.error("向网关中心注册应用接口服务异常 [systemId：" + systemId + "] ");
        }
    }
}
